package example.service;

import example.dao.UserMapper;
import example.jpa.UserRepository;
import example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

//    private List<Paper> paperList = new ArrayList<Paper>();
//    private List<Question> queslist = new ArrayList<Question>();

  @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private UserRepository userRepository;
//    @Autowired(required = false)
//    private PaperMapper paperMapper;
//
//    @Autowired(required = false)
//    private QuestionMapper questionMapper;
//
//    @Autowired(required = false)
//    private AnswerMapper answerMapper;

    public Map<String, Object> login(User user) throws Exception {

        Map<String, Object> backObject = new HashMap<String, Object>();
        User u = userMapper.login(user);
        if (u == null) {
            return backObject;
        }
/*    <!--0:待审核;1:审核通过;2:退回的问卷--> */
        backObject.put("user", u);

//
//            /*<!--我填写过的问卷-->*/
//        paperList = paperMapper.selectTianxieguo(u);
//        backObject.put("paperlist", paperList);
//
//            /*<!--0:待审核问卷;-->*/
//            if (u.getFlag().equals("1") ){
//                paperList = paperMapper.selectDaiShengHeAdmin(u);
//            }else{
//                paperList = paperMapper.selectDaiShengHeUser(u);
//            }
//
//        backObject.put("paperlist0", paperList);
//
//             /*<!--1:审核通过问卷;-->*/
//        if (u.getFlag().equals("1") ){
//            paperList = paperMapper.selectShengHeTongGuoAdmin(u);
//        }else{
//            paperList = paperMapper.selectShengHeTongGuoUser(u);
//        }
//
//        backObject.put("paperlist1", paperList);
//
//             /*<!--2:退回的问卷;-->*/
//        if (u.getFlag().equals("1") ){
//            paperList = paperMapper.selectTuiHuiAdmin(u);
//        }else{
//            paperList = paperMapper.selectTuiHuiUser(u);
//        }
//
//        backObject.put("paperlist2", paperList);
//
//        /*<!--5:审核历史;-->*/
//        paperList = paperMapper.selectShengHeLiShi(u);
//        backObject.put("paperlist5", paperList);
//
//          /*<!--9:回收的问卷;-->*/
//        paperList = paperMapper.selectHuiShou(u);
//        backObject.put("paperlist9", paperList);
//
//        /*<!--所有问卷-->*/
//        paperList = paperMapper.selectAll();
//        for (int i = 0; i < paperList.size(); i++) {
//            queslist.addAll(questionMapper.selectByPID(Long.valueOf(paperList.get(i).getPid())));
//            backObject.put("queslist", queslist);
//        }
        return backObject;
    }

//    public Map<String, Object> refleshToP(User u) {
//        Map<String, Object> backObject = new HashMap<String, Object>();
//
//        paperList = paperMapper.selectByUserForPersonal(u.getId());
//
//        backObject.put("papers", paperList);
//
//        for (int i = 0; i < paperList.size(); i++) {
//            queslist.addAll(questionMapper.selectByPID(Long.valueOf(paperList.get(i).getPid())));
//            backObject.put("queslist", queslist);
//        }
//        return backObject;
//    }

    public boolean selectByName(User user) throws Exception {

        User u = userMapper.selectByName(user);
        if (u == null) {
            return true;
        }
        return false;
    }

    public void deleteUserById(User user) throws Exception{
      userMapper.deleteUserById(user);

    }

    public User getUserByName(User user) throws Exception {
        User u = userMapper.selectByName(user);
        return u;
    }


    public List<User> getUserlistByName(User pojo) throws Exception {
        List<User> userlist;
        if (pojo==null || pojo.getName()==null || pojo.getName().equals("")){
          userlist = userMapper.getUserlistAll();
       }else{
         userlist = userMapper.getUserlistByName(pojo);
       }
        for(User tuser : userlist) {
            if (tuser.getUserimg()==null || tuser.getUserimg()==""){
                tuser.setUserimg("/upload/user.png");
            }
        }

        return userlist;

    }
    public List<User> getUserlistAll() throws Exception {

        List<User> userlist = userMapper.getUserlistAll();
        for(User tuser : userlist) {
            if (tuser.getUserimg()==null || tuser.getUserimg()==""){
                tuser.setUserimg("/upload/user.png");
            }
        }
        return userlist;
    }

    public User getUserById(long id) {
        // jpa 使用示例
        User user= userRepository.findById(id).orElse(null);
//        User user= userMapper.getUserById(id);
        if ( user.getUserimg()==null || user.getUserimg()==""){
            user.setUserimg("/upload/user.png");
        }
       return user;
    }

    public User insert(User user) {
        // userMapper.insert(user);
        userRepository.save(user);
        user= userMapper.selectByName(user);
        if ( user.getUserimg()==null || user.getUserimg()==""){
            user.setUserimg("/upload/user.png");
        }
        return user;
    }
    public User update(User user) {
        userMapper.update(user);
        user= userMapper.selectByName(user);
        if ( user.getUserimg()==null || user.getUserimg()==""){
            user.setUserimg("/upload/user.png");
        }
        return user;
    }



    public boolean insertUser(User user) {

        try {
            userMapper.insert(user);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public User getPwdByPhoneOrMail(User user) {

        try {
            user= userMapper.getPwdByPhoneOrMail(user);
            if ( user.getUserimg()==null || user.getUserimg()==""){
                user.setUserimg("/upload/user.png");
            }
            return user;

        } catch (Exception e) {
            return null;
        }

    }

    public boolean setPwdByMail(User user) {

        try {
            userMapper.setPwdByMail(user);

        } catch (Exception e) {
            return false;
        }
        return true;

    }
    public long selectCount(){

        try {
            return userMapper.selectCount();
        } catch (Exception e) {
            return 0;
        }
    }

}
